using System;
using System.Data;
using System.Configuration;

using System.Collections;
using System.Web;
using System.Web.Security;

using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;
using Component;
using System.IO;

public partial class BackEnd_ProductModify : System.Web.UI.UserControl
{
    string action = "";
    protected string onclick = "";
    public string nameImage = "";
    Product pro = null;
    public string _AU = "";
    double tyGiaVND = 0;
    public int idPro = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        this.rfvtxtNameEL.ErrorMessage = ConstString._NhapNoiDung;
        this.rfvxtNameVN.ErrorMessage= ConstString._NhapNoiDung;

        tyGiaVND = TiGiaUSDVNDDB.GetObjectByid(1, true).VND;
       string idSuCat="0";
        action = QueryHelper.GetQueryString(Request, ConstString._Action);
       
            ddlCatSub.DataSource = CategorySubDB.GetAll(true);
            ddlCatSub.DataTextField = ColumCategorySub._Name;
            ddlCatSub.DataValueField = ColumCategorySub._Id;
            this.ddlCatSub.DataBind();
            if (action == ConstString.Update)
            {
                idPro = ConvertType.ToInt(QueryHelper.GetQueryString(Request, ConstString._id));
                pro = ProductDB.GetObjectByid(idPro, true);
                idSuCat = pro.SubCat.Id.ToString();
                ddlCatSub.Items.FindByValue(idSuCat).Selected = true;

                //txtCostEL.Text = pro.USD.ToString();
                //this.lbGiaVND.Text = pro.VND.ToString();
                txtDescriptionEL.Value = pro.DescriptionEL;
                txtDescriptionVN.Value = pro.Description;
                nameImage = pro.Img;
                txtNameEL.Text = pro.NameEL;
                txtNameVN.Text = pro.Name;
                txtWeight.Text = pro.Weight;
                ddlBetSell.Text = pro.BestSeller.ToString();
                ddlNewPro.Text = pro.New.ToString();
                ddlSpePro.Text = pro.Special.ToString();
                _AU += ConstString.AUCapNhat + " " + pro.Name;
            }
            else
            {
                idSuCat = QueryHelper.GetQueryString(Request, ConstString._idsubcat);
                ddlCatSub.Items.FindByValue(idSuCat).Selected = true;
            }
            this.onclick = @"OpenWindowUploadFile(""FileManagement/File.aspx?"+Component.BackEndQuery._ProductM+"="+idPro.ToString()+"&"+ ConstString._idsubcat + "=" + idSuCat + "&id=" + this.txtImage.ClientID + @"&idimg=" + this.imageUpload.ClientID + @""",""window"",""width = 630px, height =520px"");";

    }
    public void cmdLuu_Click(object sender, System.EventArgs e)
    {
        string fileNameImage = this.txtImage.Value;
        if (action == ConstString.Update)
        {
            pro.Weight = txtWeight.Text;
            pro.USD = 1;// ConvertType.ToFloat(txtCostEL.Text.Trim());
            pro.Name = txtNameVN.Text;
            pro.NameEL = txtNameEL.Text;
            pro.Description = txtDescriptionVN.Value;
            pro.DescriptionEL = txtDescriptionEL.Value;
            pro.New = ConvertType.ToBool(ddlNewPro.Text);
            pro.Special = ConvertType.ToBool(ddlSpePro.Text);
            pro.BestSeller = ConvertType.ToBool(ddlBetSell.Text);
            pro.SubCat = CategorySubDB.GetObjectByid(ConvertType.ToInt(ddlCatSub.SelectedItem.Value), true);
            if (fileNameImage.CompareTo("") != 0)
                pro.Img = fileNameImage;
            else
                pro.Img = nameImage;
            if (ProductDB.Save(pro) == 1)
            {
                if (fileNameImage.CompareTo("") != 0)
                    File.Delete(Server.MapPath(ConvertType.ToString(nameImage).Replace("/", "\\")));
                this.lbMessage.Text = ConstString.capNhatTC;
            }
            else this.lbMessage.Text = ConstString.capNhatTB;
        }
        if (action == ConstString.Add)
        {
            Product pro = new Product(KetNoiCSDL.session);
            pro.Weight = txtWeight.Text;
            //pro.USD = ConvertType.ToFloat(txtCostEL.Text.Trim());
            //pro.VND = pro.USD * tyGiaVND;
            pro.Name = txtNameVN.Text;
            pro.NameEL = txtNameEL.Text;
            pro.SubCat = CategorySubDB.GetObjectByid(ConvertType.ToInt(this.ddlCatSub.SelectedItem.Value),true);
            pro.Description = txtDescriptionVN.Value;
            pro.DescriptionEL = txtDescriptionEL.Value;
            pro.New = ConvertType.ToBool(ddlNewPro.Text);
            pro.Special = ConvertType.ToBool(ddlSpePro.Text);
            pro.BestSeller = ConvertType.ToBool(ddlBetSell.Text);
            //pro.Img = fileNameImage;
            if (ProductDB.Save(pro) == 1)
            {
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath(ConstString._PathFileProductImg));
                if (dir.Exists)
                {
                    FileInfo[] alFile = dir.GetFiles();
                    if (alFile != null && alFile.Length > 0)
                    {
                        string directory1 = ConstString._PathFileProductImg + "//" + pro.Id.ToString();
                        Directory.CreateDirectory(Server.MapPath(directory1.Replace("//", "\\")));
                        pro.Img = directory1.Replace("//", "/") + "/" + alFile[0].Name;
                        alFile[0].MoveTo(Server.MapPath(pro.Img.Replace("//", "/").Replace("/", "\\")));
                        try
                        {
                            File.Delete(Server.MapPath(fileNameImage.Replace("/", "\\")));
                        }
                        catch { }
                    }
                }
                pro.UrlListImg = BackEndQuery._AdminQuery + BackEndQuery._ProductImgL + "&" + ConstString._idPro + "=" + pro.Id.ToString();
                pro.UrlDetailBackend = BackEndQuery._AdminQuery + BackEndQuery._ProductM + "&" + ConstString._Action + "=" + ConstString.Update + "&" + ConstString._id + "=" + pro.Id.ToString();
                pro.UrlDetailFronent = ConstString._Default + ConstString._InformationProduct + "&" + ConstString._id + "=" + pro.Id.ToString();
                ProductDB.Save(pro);
                this.lbMessage.Text = ConstString.chenTC;
                GanNullString();
            }
            else this.lbMessage.Text = ConstString.chenTB;
        }
    }
    void GanNullString()
    {
        txtDescriptionEL.Value = "";
        txtDescriptionVN.Value = "";
        txtNameEL.Text = "";
        txtNameVN.Text = "";
        txtWeight.Text = "";
    }
}
